from pymysql import connect, Error
from pymysql.cursors import DictCursor

# 用with语法改造
# with语法的好处就是打开了 可以自动关闭
try:
    # 建立连接
    conn = connect(
        host='127.0.0.1',
        port=3306,
        user='root',
        password='123456',
        database='my_news',
        # 将结果转换为字典
        cursorclass=DictCursor
    )
    with conn:
        with conn.cursor() as cursor:
            sql = "UPDATE school_student_info SET stu_name='小李明' WHERE stu_no = 1001;"
            # 执行sql
            cursor.execute(sql)
        # 提交事务
        conn.commit()
        print('执行完成')
except Error as e:
    print('连接失败：{}'.format(e))
